package com.stottlerhenke.simbionic.editor.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.stottlerhenke.simbionic.editor.ClassSpecLoader;
import com.stottlerhenke.simbionic.editor.Util;

/**
 * A dialog for importing Class Specification from Class files (.java), jars, source folder, 
 * or simbionic class specification (.txt)
 * 
 * 
 */
public class ImportClassSpecDialog extends ModalDialog{//ClassSpecGeneratorDialog{

	protected static final String TITLE = "Import class specification";
	
	protected static final String INPUT_LABEL = "Import class specification from:";
	
    protected JTextField _importFileTextField;
    
    protected ClassSpecLoader _classSpecLoader;

	private static ImportClassSpecDialog instance;
	
	public static ImportClassSpecDialog getInstance()
	{
		if (instance == null)
			instance = new ImportClassSpecDialog();
		return instance;
	}
	
	private ImportClassSpecDialog()
	{
		super(TITLE);
        this.setSize(300, this.getHeight());
	}
	
	public void setClassSpeLoader(ClassSpecLoader loader)
	{
		_classSpecLoader = loader;
	}
	
    
    protected void initGUI(){
        JPanel editPanel = new JPanel();
        editPanel.setLayout(new BoxLayout(editPanel, BoxLayout.Y_AXIS));
        editPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JLabel label = new JLabel(INPUT_LABEL);
        editPanel.add(label);
        editPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        editPanel.add(createImportFilePanel());
        editPanel.add(Box.createRigidArea(new Dimension(0, 7)));

        initDialog(editPanel, null);
        
        updateOKButtonState();
    }

    protected JPanel createImportFilePanel(){
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);
        String dialogTitle = "Input java source";
        ArrayList inputComponents = getTextFieldWithFileChooser(dialogTitle, null, null);
        _importFileTextField = (JTextField)inputComponents.get(1);
       
        FileChooserAction fileChooser = (FileChooserAction)inputComponents.get(3);
        setupFileChooser(fileChooser);
        
        _importFileTextField.getDocument().addDocumentListener(new TextFieldDocumentListener());

        return (JPanel)inputComponents.get(0);
    }
            
    protected void setupFileChooser(FileChooserAction fileChooser){
    	fileChooser.getFileChooser().addChoosableFileFilter(
    			Util.createFileFilterForExt("txt", "SimBionic Class Specification File (.txt)"));
    }
    
    private void updateOKButtonState()
    {
    	String text = _importFileTextField.getText();
    	_okButton.setEnabled(!(text == null || text.trim().length() <= 0));
    }
    
    protected void importClassSpecification()
    {
    	String importFileName = _importFileTextField.getText().trim();
    	if (importFileName.toLowerCase().endsWith(".txt"))
    	{
    		File[] files = new File[1];
    		files[0] = new File(importFileName);
    		_classSpecLoader.loadClassSpec(files);
    	}
    	else
			JOptionPane.showMessageDialog(this, 
					"Cannot import class specification from \""+importFileName+"\".", 
					"Invalid Input", JOptionPane.ERROR_MESSAGE);
    }
    
    @Override
    protected void onOk()
    {
    	super.onOk();
    	importClassSpecification();
    }
    
    class TextFieldDocumentListener implements DocumentListener
    {
    	public void changedUpdate(DocumentEvent e){ updateOKButtonState(); }
    	public void removeUpdate(DocumentEvent e){ updateOKButtonState(); }
    	public void insertUpdate(DocumentEvent e){ updateOKButtonState(); }
    }
}
